package com.gccloud.design.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gccloud.design.dao.StfAccessManagementDao;
import com.gccloud.design.entity.StfAccessManagementEntity;
import com.gccloud.design.entity.StfBusinessTypeEntity;
import com.gccloud.design.feign.BwpOperDockingClient;
import com.gccloud.design.service.IStfAccessManagementService;
import com.gccloud.design.service.IStfBusinessTypeService;
import com.gccloud.design.vo.StfAccessManagementVo;
import com.gccloud.starter.common.exception.GlobalException;
import com.gccloud.starter.common.utils.BeanConvertUtils;
import com.gccloud.starter.common.vo.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName : StfAccessManagementServiceImpl
 * @Author : sulei
 * @Date : 2020/12/23 10:29
 * @Description :
 */
@Service
public class StfAccessManagementServiceImpl extends ServiceImpl<StfAccessManagementDao, StfAccessManagementEntity> implements IStfAccessManagementService {

    @Resource
    private IStfBusinessTypeService stfBusinessTypeService;

    @Resource
    private BwpOperDockingClient bwpOperDockingClient;


    /**
     * 查询流程定制树节点
     */
    @Override
    public List getZtreeNodes(Map<String, String> params) {
        String type = "business";
        String id = params.get("id");
        if (id == null || "-2".equals(id)  || "".equals(id)) {
            List<StfAccessManagementVo> listVo = new ArrayList<>();
            List<StfAccessManagementEntity> listAccessManagement = this.listWithDp();
            for (StfAccessManagementEntity sam : listAccessManagement) {
                StfAccessManagementVo amVo =  BeanConvertUtils.convert(sam, StfAccessManagementVo.class);
                listVo.add(amVo);
            }
            return listVo;
        }
        if (params.get("nodeType") != null && !"".equals(params.get("nodeType"))) {
            type = params.get("nodeType");
        }
        if ("business".equals(type)) {
//            if(!"Y".equals(params.get("leafNode"))) {
                if(id.length()>2){
                    if("am".equals(id.substring(0, 2))){
                        Long amId=Long.parseLong(id.substring(2));
                        Map<String, Object> businessParams = new HashMap<>();
                        businessParams.put("parent_id",0);
                        businessParams.put("am_id",amId);
                        return stfBusinessTypeService.getBaseDao().selectByMapWithDp(businessParams);
                    }
                }
                QueryWrapper<StfBusinessTypeEntity> queryWrapperBusinessType = new QueryWrapper<>();
                queryWrapperBusinessType.eq("parent_id" , id);
                return stfBusinessTypeService.getBaseDao().selectListWithDp(queryWrapperBusinessType);
        }
        return null;
    }

    /**
     * 新增注册
     */
    @Override
    public void add(StfAccessManagementEntity stfAccessManagementEntity) {
        checkRepeat(stfAccessManagementEntity);
        this.save(stfAccessManagementEntity);
        // 同步新增流程平台数据(结果是两边虽然数据一样，但是主键不一样)，接收流程平台返回值主键，更新调度中心数据
        R result = bwpOperDockingClient.saveSysInsert(JSONObject.toJSONString(stfAccessManagementEntity));
        stfAccessManagementEntity.setTenantId(result.getData().toString());
        this.baseMapper.updateByIdWithDp(stfAccessManagementEntity);
    }

    @Override
    public void update(StfAccessManagementEntity stfAccessManagementEntity) {
        checkRepeat(stfAccessManagementEntity);
        StfAccessManagementEntity entity = baseMapper.selectById(stfAccessManagementEntity.getId());
        stfAccessManagementEntity.setTenantId(entity.getTenantId());
        this.baseMapper.updateByIdWithDp(stfAccessManagementEntity);
        // 同步更新流程平台stf_access_management表数据，先赋值主键
        stfAccessManagementEntity.setId(entity.getTenantId());
        bwpOperDockingClient.saveSysUpdate(JSONObject.toJSONString(stfAccessManagementEntity));
    }

    private void checkRepeat(StfAccessManagementEntity stfAccessManagementEntity) {
        boolean repeat = repeatOrg(StfAccessManagementEntity::getId, stfAccessManagementEntity.getId(), StfAccessManagementEntity::getSysNo, stfAccessManagementEntity.getSysNo());
        if (repeat) {
            throw new GlobalException(String.format("该编号重复：%s已创建", stfAccessManagementEntity.getSysNo()));
        }
    }
}
